<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" v-if="dialogVisible" width="60%">
      <el-form :model="formData" :rules="rules" ref="dataFrom" label-width="150px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="formData.name">hhh</el-input>
        </el-form-item>
        <el-form-item label="商品描述" prop="description">
          <el-input v-model="formData.description"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="formData.price"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="formData.status"></el-input>
        </el-form-item>
        <el-form-item label="照片" prop="photo">
          <el-input v-model="formData.photo"></el-input>
        </el-form-item>
        <el-form-item label="所属门类" prop="categoryId">
          <el-input v-model="formData.categoryId"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="uploadData">立即上传</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {uuid as $uuid} from "vue-uuid";
import qs from "qs";
import axios from 'axios';

export default {
  name: "updateproduct",
  data() {
    return{
      formData:{
        id:'',
        name:"",
        description:"",
        price:"",
        status:"",
        photo:"",
        categoryId:"",

      },
      rules:{
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
        ],
      },
      dialogVisible: false,
      infoData:[],
      showAll:false
    }
  },
  methods:{
    uploadData(){
      this.$refs.dataFrom.validate((valid) => {
        if (valid) {
          let that=this;
          let url=that.$URL.addproduct;

          let id=that.formData.id
          let name=that.formData.name
          let description=that.formData.description
          let price=that.formData.price
          let status=that.formData.status
          let photo=that.formData.photo
          let categoryId=that.formData.categoryId
          if (that.infoData.id!=null){
            id = that.infoData.id
          }
          if (that.infoData.name!=null){
            name = that.infoData.name
          }
          if(that.infoData.description!=null){
            description = that.infoData.description
          }
          if (that.infoData.price!=null){
            price = that.infoData.price
          }
          if (that.infoData.status!=null){
            status = that.infoData.status
          }
          if (that.infoData.photo!=null){
            photo = that.infoData.photo
          }
          if (that.infoData.categoryId!=null){
            categoryId = that.infoData.categoryId
          }
          let uploadproductParams = qs.stringify({
            id:id,
            name:name,
            description:description,
            price:price,
            status:status,
            photo:photo,
            categoryId:categoryId,
          }, { indices: false });
          console.log(uploadproductParams)

          console.log(url)
          debugger;
          let y = "http://39.96.93.100:6677/product/saveOrUpdate"
          that.$axios.post(that.$URL.addproduct, uploadproductParams).then(
              res => {
                if (res.status == 200) {
                  that.$message({
                    message: "上传成功"+res.data,

                    type: 'success'
                  });
                  //that.$refs.formData.resetFields();
                } else {
                  that.$message({
                    message: "上传失败",
                    type: 'error'
                  });
                }
              }
          );
          that.$axios.post(that.$URL.addproduct, uploadproductParams).then(
              res => {
                if (res.status == 200) {
                  that.$message({
                    message: "上传成功"+res.data,

                    type: 'success'
                  });
                  //that.$refs.formData.resetFields();
                } else {
                  that.$message({
                    message: "上传失败",
                    type: 'error'
                  });
                }
              }
          );
        }
      });
    }
  },
  created() {
    let that=this;
    this.$Bus.$on('addproduct',param=>{
      that.dialogVisible=param.dialogVisible
      that.infoData=param.infoData
      that.showAll=false
    })
  }
}
</script>

<style scoped>

</style>
